'''

Case Type： 数据库系统
Case Name： 已提交事务运行中断
Create At:  2020/06/30
Owner    :  @li-xin12345
Descption:  1.执行大量大数据量事务，提交 2.kill掉数据库进程 3.再次启动数据库，查询数据

history：
created： 2020/06/30  created by @li-xin12345
'''
import os
import time
import _thread
import queue
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Common import Common


class KillDbPid(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case103.py start------------')
        self.rootNode = Node('PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.Constant = Constant()
        self.com = Common()

    def test_dumpDataOut(self):
        # 开启事务并导入大量数据并提交
        self.log.info('----------------------------开启事务并导入大量数据并提交-----------------------------')
        sql_cmd = '''
                start transaction;
                drop table if exists t_reason;
                create table t_reason
                (
                  r_reason_sk    integer,
                  r_reason_id    character(16),
                  r_reason_desc  character(100)
                );
                insert into t_reason values (generate_series(1,1000000),'aaa', 'bbb');
                select count(*) as num from t_reason;
                commit;
                '''
        self.log.info(sql_cmd)
        msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(msg)
        self.log.info('Import Data completed')

        # kill进程
        self.log.info('----------------------------kill进程-----------------------------')
        msg = self.com.kill_pid(self.rootNode, 9)
        self.assertEqual(msg, '')

        # 重新启动数据库
        self.log.info('----------------------------重新启动数据库-----------------------------')
        is_started = self.sh_primy.startDbCluster()
        self.assertTrue(is_started)

        # 验证事务是否回滚
        self.log.info('----------------------------验证事务是否回滚-----------------------------')
        sql_cmd = 'select count(*) from t_reason;'
        self.log.info(sql_cmd)
        msg = self.sh_primy.executDbSql(sql_cmd)
        self.log.info(msg)
        self.assertIsNotNone(msg)

    def tearDownTestCase(self):
        self.log.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbClusterStatus('status')
            self.log.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()

                self.assertTrue(is_started)
                self.log.info(f'{is_started}')
                self.log.info(f'db_status: {is_started}')
        finally:
            sql_cmd = 'drop table if exists t_reason'
            self.log.info(sql_cmd)
            msg = self.sh_primy.executDbSql(sql_cmd)
            self.log.info(msg)
        self.log.info('------------------reliability_dbsys103 执行完成------------------------------')
